package com.sxmmall.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //跨域配置
        corsConfiguration.addAllowedHeader("*");   // * 所有的头都允许跨域
        corsConfiguration.addAllowedMethod("*");   // * 所有的请求方式都允许跨域
        corsConfiguration.addAllowedOrigin("*");   // * 允许任意请求来源跨域
        corsConfiguration.setAllowCredentials(true); // 允许携带cookie跨域
        source.registerCorsConfiguration("/**",corsConfiguration);  // path 所有的请求都需要配置跨域
        return new CorsWebFilter(source);
    }
}
